/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package es.fpsampayo.recursos;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.swing.JOptionPane;

 

/**
 *
 * @author fran
 */


public class Configuracion {
    
    
    
    public static void CompruebaConfigDirYFile() throws FileNotFoundException, IOException{
        
        File dir = new File(System.getProperty("user.home") + "/laya/FIN2006");
        File file = new File(dir.getAbsolutePath() + "/config.xml");
        
        if(dir.exists() == false){
            JOptionPane.showMessageDialog(null, "No existe el directorio de confiduración.\nSe procede a crear directorio y archivo de configuración en\n" + System.getProperty("user.home") + "/laya/FIN2006");
            CreaConfigDir();
            creaConfig();
        }
        if(dir.exists() == true && file.exists() == false){
            JOptionPane.showMessageDialog(null, "Parece haber desaparecido el archivo de configuración.\nSe Procede a crear uno nuevo.");
            creaConfig();
        }
    }
    
    public static void CreaConfigDir(){

        File dir = new File(System.getProperty("user.home") + "/laya/FIN2006");
        dir.mkdirs();
    }
    
    public static void creaConfig() throws FileNotFoundException, IOException{

       Properties props = new Properties();
       props.setProperty("host", "10.0.0.90");
       props.setProperty("port", "5432");
       props.setProperty("ddbb", "fin2006");

       OutputStream os = new FileOutputStream(System.getProperty("user.home") + "/laya/FIN2006/config.xml");

       props.storeToXML(os, "FIN2006 Config File", "UTF-8");

       System.out.println("XML de configuración creado correctamente");
    }

    public void leeConfig() throws FileNotFoundException, IOException{

       Properties props = new Properties();

       InputStream is = new FileInputStream(System.getProperty("user.home") + "/laya/FIN2006/config.xml");

       props.loadFromXML(is);

       String host = props.getProperty("host");

       System.out.println("La dirección de host es : '" + host + "'");

    }

    public String leeConfig (String campo) throws FileNotFoundException, IOException{

       Properties props = new Properties();

       InputStream is = new FileInputStream(System.getProperty("user.home") + "/laya/FIN2006/config.xml");

       props.loadFromXML(is);

       String valor = props.getProperty(campo);

       //System.out.println("La dirección de host es : '" + valor + "'");

       return valor;
    }

    public void guardaConfig (String campo, String valor) throws FileNotFoundException, IOException{
       Properties props = new Properties();

       InputStream is = new FileInputStream(System.getProperty("user.home") + "/laya/FIN2006/config.xml");
       props.loadFromXML(is);

       props.setProperty(campo, valor);

       OutputStream os = new FileOutputStream(System.getProperty("user.home") + "/laya/FIN2006/config.xml");

       props.storeToXML(os, "FIN2006 Config File", "UTF-8");

       System.out.println("XML de configuración guardado correctamente");
    }
}
